package com.elegantmall.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.elegantmall.dao.UserOrderDao;
import com.elegantmall.pojo.UserOrderPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserOrderServiceImpl {

    @Autowired
    private UserOrderDao userOrderDao;

    @Transactional
    public String createUserOrder(String username,String orderid, String goods, String seller) {
        UserOrderPojo order = new UserOrderPojo();
        order.setUid(STR."ORD-\{System.currentTimeMillis()}");
        order.setOrderid(orderid);
        order.setUsername(username);
        order.setGoods(goods);
        order.setSeller(seller);

        order.getOrderid();
        int result = userOrderDao.insert(order);
        return result > 0 ? "订单创建成功，订单ID: " + order.getUid() : "创建失败";
    }

    public UserOrderPojo getOrderById(String orderId) {
        if (orderId == null || orderId.isEmpty()) {
            throw new IllegalArgumentException("订单ID不能为空");
        }

        UserOrderPojo order = userOrderDao.selectById(orderId);
        if (order == null) {
            throw new IllegalArgumentException("订单不存在");
        }
        return order;
    }

    public List<UserOrderPojo> getOrdersByUsername(String username) {
        if (username == null || username.isEmpty()) {
            throw new IllegalArgumentException("用户名不能为空");
        }

        QueryWrapper<UserOrderPojo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        System.out.println("before use userOrderDao ");
        List<UserOrderPojo> res =  userOrderDao.selectList(queryWrapper);
        System.out.println("get list in service "+res);
        return res;
    }

    public List<UserOrderPojo> getOrdersBySeller(String seller) {
        if (seller == null || seller.isEmpty()) {
            throw new IllegalArgumentException("商家名称不能为空");
        }

        QueryWrapper<UserOrderPojo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("seller", seller);
        System.out.println("before use userOrderDao "+queryWrapper.getCustomSqlSegment());
        List<UserOrderPojo> res =  userOrderDao.selectList(queryWrapper);
        System.out.println("get list in userOrderDao "+res);
        return res;
    }

    @Transactional
    public String updateOrder(UserOrderPojo order) {
        if (order.getUid() == null || order.getUid().isEmpty()) {
            throw new IllegalArgumentException("订单ID不能为空");
        }

        int result = userOrderDao.updateById(order);
        if (result == 0) {
            throw new IllegalArgumentException("订单不存在或更新失败");
        }
        return "订单更新成功";
    }

    @Transactional
    public String deleteOrder(String orderId) {
        if (orderId == null || orderId.isEmpty()) {
            throw new IllegalArgumentException("订单ID不能为空");
        }

        int result = userOrderDao.deleteById(orderId);
        if (result == 0) {
            throw new IllegalArgumentException("订单不存在或删除失败");
        }
        return "订单删除成功";
    }

    @Transactional
    public String deleteUserOrder(String username, String orderId) {
        if (username == null || username.isEmpty() || orderId == null || orderId.isEmpty()) {
            throw new IllegalArgumentException("用户名和订单ID不能为空");
        }

        QueryWrapper<UserOrderPojo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username)
                .eq("orderid", orderId);

        int result = userOrderDao.delete(queryWrapper);
        if (result == 0) {
            throw new IllegalArgumentException("订单不存在或删除失败");
        }
        return "用户订单删除成功";
    }
}